package cn.iocoder.yudao.module.ym.controller.admin.sitedata;

import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
import cn.iocoder.yudao.module.ym.controller.admin.area.vo.AreaRespVO;
import cn.iocoder.yudao.module.ym.controller.admin.site.vo.SiteImportVO;
import cn.iocoder.yudao.module.ym.controller.admin.site.vo.SitePageReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ym.controller.admin.sitedata.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.sitedata.SiteDataDO;
import cn.iocoder.yudao.module.ym.service.sitedata.SiteDataService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 电站数据")
@RestController
@RequestMapping("/ym/site-data")
@Validated
public class SiteDataController {

    @Resource
    private SiteDataService siteDataService;

    @PostMapping("/create")
    @Operation(summary = "创建电站数据")
    public CommonResult<Long> createSiteData(@Valid @RequestBody SiteDataSaveReqVO createReqVO) {
        return success(siteDataService.createSiteData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电站数据")
    public CommonResult<Boolean> updateSiteData(@Valid @RequestBody SiteDataSaveReqVO updateReqVO) {
        siteDataService.updateSiteData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电站数据")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSiteData(@RequestParam("id") Long id) {
        siteDataService.deleteSiteData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电站数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SiteDataRespVO> getSiteData(@RequestParam("id") Long id) {
        SiteDataDO siteData = siteDataService.getSiteData(id);
        return success(BeanUtils.toBean(siteData, SiteDataRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得电站数据分页")
    public CommonResult<List<SiteDataRespVO>> getSiteDataList(@Valid SiteDataPageReqVO pageReqVO) {
        List<SiteDataDO> pageResult = siteDataService.getSiteDataList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SiteDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电站数据分页")
    public CommonResult<PageResult<SiteDataRespVO>> getSiteDataPage(@Valid SiteDataPageReqVO pageReqVO) {
        PageResult<SiteDataDO> pageResult = siteDataService.getSiteDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SiteDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电站数据 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSiteDataExcel(@Valid SiteDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SiteDataDO> list = siteDataService.getSiteDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电站数据.xls", "数据", SiteDataRespVO.class,
                        BeanUtils.toBean(list, SiteDataRespVO.class));
    }

    @PostMapping("/excel-import")
    public CommonResult<UserImportRespVO> uploadExcel(@RequestParam("file") MultipartFile file, Boolean updateSupport) throws IOException {
        // 2. 解析Excel数据
        List<SiteDataImportVO> read = ExcelUtils.read(file, SiteDataImportVO.class);
        return success(siteDataService.importSiteDate(read,updateSupport));

    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得电站数据导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<SiteDataRespVO> list = Arrays.asList(
                SiteDataRespVO.builder()
                        .siteName("南海桂城天安数码站")
                        .year(2025)
                        .month(3)
                        .electricQuantity(new BigDecimal("10840.88"))
                        .fastChargeQuantity(new BigDecimal("10840.88"))
                        .slowChargeQuantity(new BigDecimal("0.00"))
                        .ratedPower(new BigDecimal("357120.00"))
                        .powerUtilization(new BigDecimal("0.4321"))
                        .commPower(new BigDecimal("480.00"))
                        .pfFee(new BigDecimal("0.00"))
                        .elecCost(new BigDecimal("6754.33"))
                        .powerLoss(new BigDecimal("500.40"))
                        .lossRate(new BigDecimal("0.1741"))
                        .deprec(new BigDecimal("924.72"))
                        .opCostShare(new BigDecimal("927.63"))
                        .opCostRent(new BigDecimal("0.00"))
                        .opCostOther(new BigDecimal("0.00"))
                        .opCostLabor(new BigDecimal("0.00"))
                        .opCostFacility(new BigDecimal("0.00"))
                        .maintCost(new BigDecimal("0.00"))
                        .opCostAll(new BigDecimal("927.63"))
                        .elecRev(new BigDecimal("6253.93"))
                        .edDiscount(new BigDecimal("0.00"))
                        .servFeeRev(new BigDecimal("1606.21"))
                        .valueAddServ(new BigDecimal("0.00"))
                        .compServRev(new BigDecimal("1606.21"))
                        .selfOpGp(BigDecimal.valueOf(-746.54))
                        .otherRev(new BigDecimal("0.00"))
                        .otherCost(new BigDecimal("0.00"))
                        .selfOpAll(new BigDecimal(-746.54))
                        .slowCapacity(new BigDecimal("1145.14"))
                        .fastCapacity(new BigDecimal("1145.14"))
                        .build());
        // 输出
        ExcelUtils.write(response, "电站数据导入模板.xls", "区域列表", SiteDataRespVO.class, list);
    }

    @GetMapping("/sumList")
    @Operation(summary = "获取数据整合")
    public CommonResult<List<MonthlyCostVO>> sumList(SiteDataPageReqVO reqVO){
        List<SiteDataPageReqVO> siteDataPageReqVOS = siteDataService.sumList(reqVO);
        if(siteDataPageReqVOS != null && !siteDataPageReqVOS.isEmpty()){
            List<MonthlyCostVO> convertedList = ListConverter.convert(siteDataPageReqVOS);
            return success(convertedList);
        }
        return success(Collections.emptyList());
    }

    @GetMapping("/annualList")
    @Operation(summary = "获取全年综合数据")
    public CommonResult<SiteDataPageReqVO> annualList(SiteDataPageReqVO reqVO){
        List<SiteDataPageReqVO> siteDataPageReqVOS = siteDataService.sumList(reqVO);
        return success(siteDataPageReqVOS.get(12));
    }

    @GetMapping("/chartData")
    @Operation(summary = "获取上月的图表数据")
    public CommonResult<SiteDataPageReqVO> chartData(SitePageReqVO pageReqVO){
        Map<Object, Object> objectObjectMap = siteDataService.averageCapacity(pageReqVO);
        return success(siteDataService.chartData(pageReqVO));
    }

    @GetMapping("/dataStatistics")
    @Operation(summary = "获取数据看板上方数据")
    public CommonResult<Map<Object,Object>>  dataStatistics (SitePageReqVO pageReqVO1){
        return success(siteDataService.dataStatistics(pageReqVO1));
    }


    @GetMapping("/annualData")
    @Operation(summary = "获取本年总和数据")
    public CommonResult<Map<Integer, Object>> annualData(SitePageReqVO sitePageReqVO){
        return success(siteDataService.annualData(sitePageReqVO));
    }


    @GetMapping("/dateGroupingSelect")
    @Operation(summary = "获取毛利，运能，待处理工单，总工单 1上月 2上年 3全部")
    public CommonResult<Map<Integer,Object>> dateGroupingSelect(Integer integer,Long siteId){
        return success(siteDataService.dateGroupingSelect(integer,siteId));
    }



    @GetMapping("/lineChart")
    @Operation(summary = "获取数据集中的利润和人工成本")
    public CommonResult<List<MonthlyCostVO>> lineChart(SiteDataPageReqVO reqVO){
        return success(siteDataService.lineChart(reqVO));
    }
}
